<?php 

require 'ask.lang.php';
//if(!$_userid) showmessage($LANG['please_login'],url("?module=member&action=login&forward=".urlencode(URL)));

if(!$_userid) showmessage($LANG['please_login'],url("?module=member&action=login&forward=".urlencode(URL)));

require 'ask.class.php';
$ask = new ask();
require_once 'credit.class.php';
$credit = new credit();
require_once 'answer.class.php';
$answer = new answer();
require_once MOD_ROOT.'member/includes/member.class.php';
$member = new member();

if(!$forward) $forward = HTTP_REFERER;

include 'ask.func.php';
include 'form.class.php';

$C = subcat('ask');

cache_page_start();
$solve_ques_count = solve_ask_count(1);
$nosolve_ques_count = solve_ask_count(0);

$id = intval($id);
if(!isset($action)) $action = '';

//if(!isset($action)) $action = 'ask';
switch($action)
{
	case 'ask':
		if(isset($status)) $status = intval($status);
		if(isset($flag)) $flag = intval($flag);
		$sql = 'userid='.$_userid;
		if($status == 3 || $status == 5)
		{
			$sql .= " AND status=".$status;
		}
		else if($flag == 1)
		{
			$sql .= ' AND flag=1';
		}
		else if($flag == -1)
		{
			$endtime = TIME-1296000;
			$sql .= ' AND status = 3 AND addtime<'.$endtime;
		}
		$infos = $ask->listinfo($sql, 'askid DESC', $page, 20);
		$pages = $ask->pages;
		$head['title'] = '提问管理_问吧_'.$SETTING['sitename'];
		include template('center_ask.html', 'ask');
	break;

	case 'answer':
		$infos = $answer->listinfo("userid='$_userid' AND isask=0", 'pid DESC', $page, 20);
		$pages = $answer->pages;
		$head['title'] = '我的回答_问吧_'.$SETTING['sitename'];
		include template('center_answer.html','ask');
	break;

	case 'actor':
		if($dosubmit)
		{
			$member->edit($info);
			showmessage($LANG['operation_success'],$forward);
		}
		else
		{
			$array = $member->get($_userid,'i.actortype',1);
			$actortype = $array['actortype'];
			$TYPES = explode("\n", $M['member_group']);
			$ACTOR = cache_read('actor.php');
		    $head['title'] = '修改头衔_问吧_'.$SETTING['sitename'];
			include template('center_actor.html','ask');
		}
	break;

	case 'credit':
		$asknumber = $ask->getnumber($_userid,0);
		$answernumber = $ask->getnumber($_userid,1);
		$result = $credit->get($_userid);
		@extract($result);
		$array = $member->get($_userid,'i.actortype',1);
		$actortype = $array['actortype'];
		$head['title'] = '查看积分_问吧_'.$SETTING['sitename'];
		include template('ask', 'center_credit');
	break;

	case 'edit':
		if($job=='ask')
		{
			if($dosubmit)
			{
				if($info['title'] == '') showmessage($LANG['title_no_allow_blank'],'goback');
				$info['title'] = htmlspecialchars($info['title']);
				$posts['message'] = $M['use_editor'] ? $posts['message'] : strip_tags($posts['message']);
				$ask->edit($id, $info, $posts, $_userid);
				showmessage($LANG['operation_success'],$forward);
			}
			else
			{
				$r = $ask->detail($id,'a.title,p.message,a.status', 1);
				if(!$r) showmessage('提问不存在');
				extract($r);
				if($status>3) showmessage($LANG['no_edit'],"goback");
		        $head['title'] = '修改提问_问吧_'.$SETTING['sitename'];
				include template('center_edit.html','ask');
			}
		}
		else
		{
			if($dosubmit)
			{
				$answer->edit($id, $posts, $_userid);
				showmessage($LANG['operation_success'], $forward);
			}
			else
			{
				$r = $answer->get($id, 'a.status,p.message', 1);
				if(!$r) showmessage('回答不存在');
				extract($r);
		        $head['title'] = '修改回答_问吧_'.$SETTING['sitename'];
				include template('center_edit.html','ask');
			}

		}
	break;
	case 'getcheckcode':
		echo form::checkcode('checkcodestr',5);
	break;
	
	case 'question':
	
		if($dosubmit)
		{
			checkcode($checkcodestr, $M['publish_code']);
			$info['catid'] = intval($info['catid']);
			$info['reward'] = intval($info['reward']);
			$info['title'] = htmlspecialchars($info['title']);
			if($info['title'] == '') showmessage($LANG['title_no_allow_blank'],'goback');
			if(!$info['catid']) showmessage($LANG['select_category'],'goback');
			if($info['reward'] > $_point) showmessage($LANG['credit_is_poor'],'goback');
			$posts['isask'] = 1;
			foreach($info AS $key=>$val)
			{
				if(!in_array($key,array('title','catid','reward','anonymity'))) unset($info[$key]);
			}
			$posts['message'] = $M['use_editor'] ? $message : strip_tags($message);
			$info['addtime'] = $posts['addtime'] = TIME;
			$info['endtime'] = TIME+1296000;
			$info['userid'] = $posts['userid'] = $_userid;
			$info['username'] = $posts['username'] = $_username;

			if($posts['anonymity'] && intval($M['anybody_score']+$info['reward'])>$_point)
			{
				$posts['anonymity'] = 0;
			}
			if(file_exists(CACHE_PATH.'cache_ask_update.php'))
			{
				$cache_ask_update = file_get_contents(CACHE_PATH.'cache_ask_update.php');
				if(!empty($cache_ask_update) && $cache_ask_update < (TIME-intval($M['autoupdate'])))
				{
					require MOD_ROOT.'cache/cache.func.php';
					cache_common();
					file_put_contents(CACHE_PATH.'cache_ask_update.php',TIME);
					@chmod(CACHE_PATH.'cache_ask_update.php');
				}
			}
			else
			{
				file_put_contents(CACHE_PATH.'cache_ask_update.php',TIME);
				@chmod(CACHE_PATH.'cache_ask_update.php');
			}
			if($M['publish_check'])
			{
				$info['status'] = $posts['status'] = 1;
				$id = $ask->add($info,$posts);
				showmessage($LANG['waiting_check'],$forward);
			}
			else
			{
				$info['status'] = $posts['status'] = 3;
				if($info['reward'] >= $M['height_score']) $info['flag'] = 2;
				$id = $ask->add($info,$posts); 
				showmessage($LANG['publish_success'],url("?module=ask&action=view&id=$id"));
			}
		}
		else
		{
			$title = stripslashes($title);
			//if($ask->check_status()>0) showmessage($LANG['please_solve_expired_ask'], url("?module=ask&action=ask&flag=-1"));
			$head['title'] = '我要提问';
			include template('question.html', 'ask');
		}
	break;
	
	case 'vote':
		$infos = $answer->vote_result($id);
		$num = count($infos);
		include template('view_vote.html', 'ask');
	break;
	
	case 'browse':
	
		$head['keywords'] = $M['name'];
		$head['description'] = $head['title'] = $M['name'].' - '.$SETTING['sitename'];
		
		include template('browse.html','ask');
	break;
	
	case 'creditlist':
	
		$head['keywords'] = $LANG['credit_list'];
		$head['description'] = $head['title'] = $LANG['credit_list'].' - '.$SETTING['sitename'];
		include template('credit_list.html','ask');
	break;
	
	case 'search':
		
		require_once 'ask.class.php';
		$ask = new ask();
		$where = '';
		$status = $status ? intval($status) : 3;
		if($status==5)
		{
			$where .= ' status=5';
		}
		else if($flag==1)
		{
			$where .= ' status=3 AND flag=1';
		}
		else
		{
			$where .= ' status=3';
		}
		if($keywords)
		{
			$where .= " AND title LIKE '%$keywords%'";
		}
		$infos = $ask->listinfo($where, 'askid DESC', $page, 20);
		$pages = $ask->pages;
		
		$head['title'] = $keywords.'_问吧搜索_'.$SETTING['sitename'];
		$head['keywords'] = $keywords;
		$head['description'] = $keywords;
		include template('search.html','ask');
	break;
	
	case 'search_ajax':
		
		require_once MOD_ROOT.'include/ask.class.php';
		$ask = new ask();
		header('Content-type: text/html; charset=utf-8');
		if(strtolower(CHARSET) != 'utf-8') $q = iconv(CHARSET, 'utf-8', $q);
		$q = addslashes($q);
		if($q)
		{
			$where = " title LIKE '%$q%' AND status = 5";
		}
		else 
		{
			exit('null');
		}
		$infos = $ask->listinfo($where, 'askid DESC', '', 10);
		
		foreach($infos as $key=>$val) 
		{
			$val['title'] = str_replace($q, '<span class="c_orange">'.$q.'</span>', $val['title']);
			$info[$key]['title'] = CHARSET != 'utf-8' ? iconv(CHARSET, 'utf-8', $val['title']) : $val['title'];
			$info[$key]['url'] = $val['url'];
		}
		
		echo(json_encode($info));
	break;
	
	case 'load':
		$id = $id ? $id : 0;
		$CATEGORY = subcat('ask');
		$str .= '<select><option value="0" selected>请选择</option>';
		$options = '';
		foreach($CATEGORY as $i=>$v)
		{
			if((isset($id) && $v['parentid'] == $id) || (isset($module) && $v['module'] == $module))  $options .= '<option value="'.$v['catid'].'">'.$v['catname'].'</option>';
		}
		if(empty($options)) exit;
		$str .= $options.'</select>';
		
		echo $str;
	break;
	
	case 'list':
		
		$catid = intval($catid);
		$catid or showmessage($LANG['illegal_parameters'], SITE_URL);
		if(!array_key_exists($catid, $CATEGORY) || $CATEGORY[$catid]['module']!=$module) showmessage($LANG['illegal_parameters'],'goback');
		
		$CAT = cache_read('category_'.$catid.'.php');
		@extract($CAT);
		
		$head['keywords'] = $catname;
		$head['description'] = $head['title'] = $catname.'_'.$M['name'].'_'.$SETTING['sitename'];
		
		$array = array('all','vote','solve','high','nosolve');
		foreach($array AS $arr)
		{
			$$arr = caturl($arr);
		}
		
		if($child==1)
		{
			$arrchildid = subcat('ask',$catid);
			include template('category.html','ask');
		}
		else
		{
			include template('category_list.html','ask');
		}
		
	break;
	
	case 'view':
		if($dosubmit && $action=='vote')
		{
			if(!$_userid) showmessage($LANG['please_login'],url("?module=member&action=login&forward=".urlencode(URL)));
			$answer->exchange($id,$check_answer, 1, 1, $_userid);
			showmessage($LANG['exchange_ask_to_vote'],"?module=ask&action=view&id=$id");
		}
		else if($dosubmit)
		{
			checkcode($checkcodestr, $M['answer_code'], $forward);
			if(!$_userid) showmessage($LANG['please_login'],url("?module=member&action=login&forward=".urlencode(URL)));
			if(!$id) showmessage($LANG['illegal_parameters']);
			if(strlen($posts['message']) > 10000) showmessage('回答字数不能超过10000个字符');
			$posts['userid'] = $_userid;
			$posts['username'] = $_username;
			if($M['answer_check'])
			{
				$posts['status'] = 1;
			}
			else
			{
				$posts['status'] = 3;
			}
			$posts['addtime'] = TIME;
			$posts['message'] = $M['use_editor'] ? $posts['message'] : strip_tags($posts['message']);
			if($answer->add($id,$posts))
			{
				if($M['answer_check'])
				{
					showmessage($LANG['waitting_admin_check'],$forward);
				}
				else
				{
					showmessage($LANG['your_answer_submit_success'],$forward);
				}
			}
			else
			{
				showmessage('您已经回答了这个问题，您的答案正在审核当中，请等待...');
			}
		}
		else
		{
			$array = $answer->show($id);
			if($array)
			{
				$answer->hits($id);
				$have_answer = false;
				foreach($array AS $k=>$v)
				{
					if($v['isask'])
					{
						$title = $v['title'];
						$message = $M['use_editor'] ? $v['message'] : format_textarea($v['message']);
						$reward = $v['reward'];
						$userid = $v['userid'];
						$username = $v['username'];
						$status = $v['status'];
						$flag = $v['flag'];
						$addtime = $v['addtime'];
						$actor = actor($v['actortype'], $v['point']);
						$answercount = $v['answercount'];
						$result = count_down($v['endtime']);
						$day = $result[0];
						$hour = $result[1];
						$minute = $result[2];
						$catid = $v['catid'];
						$anonymity = $v['anonymity'];
					}
					elseif($v['optimal'])
					{
						$solvetime = $v['solvetime'];
						$answer = $v['message'];
						$answertime = $v['addtime'];
						$answer = $v['message'];
						$optimail_username = $v['username'];
						$optimal_actor = actor($v['actortype'], $v['point']);
					}
					else
					{
						if($v['userid'] == $_userid) $have_answer = true;
						$infos[$k]['pid'] = $v['pid'];
						$infos[$k]['userid'] = $v['userid'];
						$infos[$k]['username'] = $v['username'];
						$infos[$k]['addtime'] = $v['addtime'];
						$infos[$k]['candidate'] = $v['candidate'];
						$infos[$k]['anonymity'] = $v['anonymity'];
						$infos[$k]['actor'] = actor($v['actortype'], $v['point']);
						$infos[$k]['message'] = $M['use_editor'] ? $v['message'] : format_textarea($v['message']);
					}
				}
				if($v['optimal'])
				{
					$answercount = $answercount-1;
				}
				if($userid == $_userid)
				{
					$isask = 1;
				}
				else
				{
					$isask = 0;
				}
				if(isset($action) && $action == 'vote')
				{
					if($flag==1) exit;
					$tpl = 'vote.html';
				}
				else
				{
					$tpl = 'show.html';
				}
				$head['keywords'] = $title;
				$head['description'] = $head['title'] = $title.'_'.$CATEGORY[$catid]['catname'].'_'.$M['name'].'_'.$SETTING['sitename'];
				include template($tpl,'ask');
			}
		}
	break;
	
	case 'accept_answer':
	$ask->accept_answer($id, $pid);
	$forward = $forward ? $forward : '';
	showmessage($LANG['optimal_answer'], "?module=ask&action=view&id=$id");
	break;

	case 'over':
	$ask->status($id, 6, $_userid);
	if($M['return_credit'])
	{
		extract($db->get_one("SELECT userid,ischeck FROM $ask->table WHERE askid=$id"));
		if($ischeck) $ask->pay->update_exchange('ask', 'point', $M['return_credit'], $LANG['return_credit'], $userid);
	}
	showmessage($LANG['ask_finish'], "?module=ask&action=view&id=$id");
	break;

	case 'vote':
	if($answer->vote($id, $pid))
	{
		echo $LANG['thinks_your_vote'];
	}
	else
	{
		echo $LANG['your_have_vote'];
	}
	break;

	case 'edit_answer':
	if($dosubmit)
	{
		if(strlen($answertext) > 10000) showmessage('回答字数不能超过10000个字符');
		$posts['message'] = $M['use_editor'] ? $answertext : strip_tags($answertext);
		$answer->edit($pid, $posts, $_userid);
		showmessage($LANG['operation_success'], $forward);
	}
	break;

	case 'edit_ask':
	if($dosubmit)
	{
		$info['title'] = htmlspecialchars($title);
		if(strlen($title)<2) showmessage($LANG['title_is_short']);
		$posts['message'] = $M['use_editor'] ? $message : strip_tags($message);
		$ask->edit($id, $info, $posts, $_userid);
		showmessage($LANG['operation_success'], $forward);
	}
	break;

	case 'addscore':
	if($dosubmit)
	{
		$ask->addscore($id, $point);
		showmessage($LANG['operation_success'], $forward);
	}
	break;	
	
	default:
	 
		
		$head['keywords'] = $M['name'];
		$head['description'] = $head['title'] = $M['name'].'_'.$SETTING['sitename'];
		
		
		include template('index.html','ask');
		cache_page(intval($M['autoupdate']));
		
	break;
	
}
?>